/**
 * Native browser control based on HTML "select" element.
 * Note: this one is a problem for styling. If you want to have a custom list box
 * e.g. with custom borders, background color etc., you should use ComboBox class instead.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see core.ui.ComboBox
 */

import core.ui.Window;

class ListBox extends Window {
	
	/**
	 * Sets the list box multiple selectable.
	 *
	 * @param multiple (bool) true if multiple, false if single
	 */
	this.setMultiple = function(multiple) {
		this.getElement().multiple = multiple;
	}
	
	/**
	 * Checks whether the list box is multiple selectable.
	 *
	 * @return bool
	 */
	this.isMultiple = function() {
		return this.getElement().multiple;
	}
	
	/**
	 * Adds new list box item.
	 *
	 * @param text (string) item text
	 * @param value (string) item value
	 * @param selected (bool) [optional] whether the item is selected (false by default)
	 */
	this.addItem = function(text, value, selected) {
		this.insertItem(this.getElement().options.length, text, value, selected);
	}
	
	/**
	 * Inserts new item to the list box.
	 * 
	 * @param index (int) item position
	 * @param text (string) item text
	 * @param value (string) item value
	 * @param selected (bool) [optional] whether the item is selected (false by default)
	 */
	this.insertItem = function(i, text, value, selected) {
		var option = new Option(text);
		option.value = value;
		option.selected = selected;
		this.getElement().options[i] = option;
	}
	
	/**
	 * Removes item with specified index.
	 * 
	 * @param index (int) item index
	 */
	this.removeItem = function(i) {
		this.getElement().remove(i);
	}
	
	/**
	 * Returns currently selected item index.
	 *
	 * @return int
	 */
	this.getSelectedIndex = function() {
		return this.getElement().selectedIndex;
	}
	
	/**
	 * Returns currently selected item.
	 *
	 * @return HTMLOptionElement
	 */
	this.getSelectedItem = function() {
		return this.getElement().options[this.getSelectedIndex()].text;
	}
	
	/**
	 * Returns currently selected item value.
	 *
	 * @return string
	 */
	this.getSelectedValue = function() {
		return this.getElement().options[this.getSelectedIndex()].value;
	}
	
	/**
	 * @protected
	 * Creates and returns root element for the list box.
	 * 
	 * @return HTMLSelectElement
	 */
	this.createElement = function() {
		return document.createElement('select');
	}
}